package com.powernode.customer.controller;

import com.powernode.common.annotation.PowerLogin;
import com.powernode.common.constant.RedisConstant;
import com.powernode.common.result.Result;
import com.powernode.common.util.AuthContextHolder;
import com.powernode.customer.service.impl.CustomerServiceImpl;
import com.powernode.model.form.customer.UpdateWxPhoneForm;
import com.powernode.model.vo.customer.CustomerLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "客户API接口管理")
@RestController
@RequestMapping("/customer")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerController {

    @Resource
    private CustomerServiceImpl customerServiceImpl;

    @Resource
    private RedisTemplate redisTemplate;

    @Operation(summary = "小程序登录")
    @GetMapping("/login/{code}")
    public Result<String> login(@PathVariable String code){
        return Result.ok(customerServiceImpl.login(code));
    }


    @PowerLogin//aop
    @Operation(summary = "获取客户登录信息")
    @GetMapping("/getCustomerLoginInfo")
    public Result<CustomerLoginVo> getCustomerLoginInfo(@RequestHeader("token") String token){
        //根据token从redis中获取用户id
        //String customerId = (String)redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);

        //本质：从ThreadLocal中获取的用户id  jvm内存中获取
        Long customerId = AuthContextHolder.getUserId();
        //后续不再使用ThreadLocal,最好将其删除
        AuthContextHolder.removeUserId();


        return Result.ok(customerServiceImpl.getCustomerLoginInfo(customerId));
    }


    @PowerLogin
    @Operation(summary = "更新手机号码")
    @PostMapping("/updateWxPhone")
    public Result<Boolean> updateWxPhone(@RequestBody UpdateWxPhoneForm updateWxPhoneForm){
//        return Result.ok(customerServiceImpl.updateWxPhoneNumber(updateWxPhoneForm));

        //个人开发者获取不到手机号,所以这里写死
        return Result.ok(true);
    }
}

